
package com.stnet.dao.enterprice.system;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;

import com.modules.commons.Pager;
import com.stnet.bases.BaseDaoImpl;
import com.stnet.entity.Authorities;
import com.stnet.entity.AuthoritiesBean;

public class AuthoritiesDaoImpl extends BaseDaoImpl
    implements AuthoritiesDao
{

    public AuthoritiesDaoImpl()
    {
    }

    public void delete(Authorities authorities)
    {
        getHibernateTemplate().delete(authorities);
    }

    public void delete(Long id)
    {
        getHibernateTemplate().delete(get(id));
    }

    public List findAll()
    {
        List find = getHibernateTemplate().find("from Authorities order by orderAuth asc");
        return find;
    }

    public Authorities get(Long id)
    {
        return (Authorities)getHibernateTemplate().get(Authorities.class, id);
    }

    public void save(Authorities authorities)
    {
        getHibernateTemplate().save(authorities);
    }

    public void update(Authorities authorities)
    {
    	//this.getSessionFactory().getCurrentSession().clear();
        getHibernateTemplate().merge(authorities);
    }

    public boolean isNameUnique(String name)
    {
        List temp = getHibernateTemplate().find("from Authorities where name = ?  order by orderAuth asc", name.trim());
        return temp.size() <= 0;
    }
    
    public Authorities getByName(String name)
    {
        List temp = getHibernateTemplate().find("from Authorities where name = ?", name.trim());
        if(temp.size()>0){
        	return (Authorities)temp.get(0);
        }
        return null;
    }

    public boolean isDisplayNameUnique(String displayName)
    {
        List temp = getHibernateTemplate().find("from Authorities where displayName = ?", displayName.trim());
        return temp.size() <= 0;
    }

    public List findAuthoritiesByTypeId(Long id)
    {
        List temp = getHibernateTemplate().find("from Authorities where authoritiesId = ?", id);
        return temp;
    }
    
    /**
     * 重写
     * */
    public Pager getObjectListByClass(int pageSize, int pageNo, String statu) {
		Session session =null;
		Pager pager = null;
		try
		{
			session = getHibernateTemplate().getSessionFactory().openSession();
			Criteria criteria = session.createCriteria(Authorities.class);
			if (!"-1".equals(statu))
				criteria.add(Restrictions.eq("state", statu));
			
			criteria.addOrder(Order.asc("orderAuth"));
			criteria.addOrder(Order.desc("id"));
			int rowCount = ((Integer) criteria.setProjection(Projections.rowCount()).uniqueResult()).intValue();
			criteria.setProjection(null);
			int startIndex = pageSize * (pageNo - 1);
			criteria.setFirstResult(startIndex);
			criteria.setMaxResults(pageSize);
			List result = criteria.list();
			pager = new Pager(pageSize, pageNo, rowCount, result);
		}catch(Exception ex)
		{
			ex.printStackTrace();
		}finally
		{
			session.close();
		}
		
		return pager;
	}
    
    public Pager findAuthoritiesByType(int pageSize, int pageNo,String state,String type) {
		Session session =null;
		Pager pager = null;
		try
		{
			session = getHibernateTemplate().getSessionFactory().openSession();
			Criteria criteria = session.createCriteria(Authorities.class);
			if (!"-1".equals(state))
				criteria.add(Restrictions.eq("state", state));
			if (!"-1".equals(type))
				criteria.add(Restrictions.eq("authoritiesType", type));
			
			criteria.addOrder(Order.asc("orderAuth"));
			
			int rowCount = ((Integer) criteria.setProjection(Projections.rowCount()).uniqueResult()).intValue();
			criteria.setProjection(null);
			int startIndex = pageSize * (pageNo - 1);
			criteria.setFirstResult(startIndex);
			criteria.setMaxResults(pageSize);
			List result = criteria.list();
			pager = new Pager(pageSize, pageNo, rowCount, result);
		}catch(Exception ex)
		{
			
		}finally
		{
			session.close();
		}
		
		return pager;
	}
    public List findAuthorities(String type,String salesDirect){
    	String hql = "from Authorities where authoritiesType = ? ";
    	
    	if(!"0".equals(salesDirect))
    		hql = hql + " and  salesDirect = " + salesDirect;
    	List temp = getHibernateTemplate().find(hql, type);
        return temp;
    }
    
    public List<AuthoritiesBean> findAuthoritiesByRose(String type,String salesDirect,Long roseid) {
    	String hql = "select new AuthoritiesBean(a.id,a.name) from Authorities a , "+
    	"RolesAuthorities ra where a.id=ra.authorities.id and ra.roles.id = ? and "+
    	" a.authoritiesType = ? and a.salesDirect = ? order by a.orderAuth asc";
    	
    	System.err.print(hql);
    	List<AuthoritiesBean> temp = getHibernateTemplate().find(hql, new Object[]{roseid,type,salesDirect});
        return temp;
    }
  
}